/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.UserAccountBill;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.UserAccountBillRepository;
import me.zhengjie.modules.system.service.UserAccountBillService;
import me.zhengjie.modules.system.service.dto.UserAccountBillDto;
import me.zhengjie.modules.system.service.dto.UserAccountBillQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.UserAccountBillMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wangyu
* @date 2024-03-20
**/
@Service
@RequiredArgsConstructor
public class UserAccountBillServiceImpl implements UserAccountBillService {

    private final UserAccountBillRepository userAccountBillRepository;
    private final UserAccountBillMapper userAccountBillMapper;

    @Override
    public Map<String,Object> queryAll(UserAccountBillQueryCriteria criteria, Pageable pageable){
        Page<UserAccountBill> page = userAccountBillRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userAccountBillMapper::toDto));
    }

    @Override
    public List<UserAccountBillDto> queryAll(UserAccountBillQueryCriteria criteria){
        return userAccountBillMapper.toDto(userAccountBillRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UserAccountBillDto findById(Long billId) {
        UserAccountBill userAccountBill = userAccountBillRepository.findById(billId).orElseGet(UserAccountBill::new);
        ValidationUtil.isNull(userAccountBill.getBillId(),"UserAccountBill","billId",billId);
        return userAccountBillMapper.toDto(userAccountBill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserAccountBillDto create(UserAccountBill resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setBillId(snowflake.nextId()); 
        return userAccountBillMapper.toDto(userAccountBillRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserAccountBill resources) {
        UserAccountBill userAccountBill = userAccountBillRepository.findById(resources.getBillId()).orElseGet(UserAccountBill::new);
        ValidationUtil.isNull( userAccountBill.getBillId(),"UserAccountBill","id",resources.getBillId());
        userAccountBill.copy(resources);
        userAccountBillRepository.save(userAccountBill);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long billId : ids) {
            userAccountBillRepository.deleteById(billId);
        }
    }

    @Override
    public void download(List<UserAccountBillDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserAccountBillDto userAccountBill : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("系统用户ID", userAccountBill.getSysUserId());
            map.put("账户编号", userAccountBill.getAcctNo());
            map.put("用户openid", userAccountBill.getOpenId());
            map.put("类型，0：收入，1：提现，2：消费支出", userAccountBill.getBillType());
            map.put("变更金额", userAccountBill.getAmount());
            map.put("三方流水号", userAccountBill.getThdFlowid());
            map.put("审批状态(0：提交申请 1： 处理中 2：成功 3：驳回)", userAccountBill.getApprovalState());
            map.put("提现状态 (0: 待处理,1:处理中，2成功，3：失败,4驳回", userAccountBill.getCashoutState());
            map.put("原因", userAccountBill.getFailMsg());
            map.put("业务编号", userAccountBill.getBizNo());
            map.put("业务来源", userAccountBill.getBizSource());
            map.put("创建时间", userAccountBill.getCreateTime());
            map.put("账单状态0：无效，1：有效，2：过期，3：作废", userAccountBill.getBillStatus());
            map.put("账单过期时间", userAccountBill.getExpireTime());
            map.put("账单备注", userAccountBill.getBillRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}